/************************************************************************************/
/*  Program name CAPFNDIMP_CONC_PROG_CONTEXT.sql                                    */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER FND_DESCR_FLEX_CONTEXTS_CT1 DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFNDIMP_CONC_PROG_CONTEXT';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FND_CONC_PROG_CONTEXT;

	CURSOR Cur_G IS
	SELECT
		A.*
	FROM
		CFND_DESCR_FLEX_CONTEXTS	A,
		CRST_FND_CONC_PROG_CONTEXT	B
	WHERE	A.Application_ID				=	B.Application_ID
	AND		A.Descriptive_Flexfield_Name	=	B.Descriptive_Flexfield_Name
	AND		A.Descriptive_Flex_Context_Code	=	B.Descriptive_Flex_Context_Code;

	lv_Next_Seq_ID	NUMBER;

BEGIN

	--  RENAME UNIQUE KEY ?
	UPDATE
		FND_DESCR_FLEX_CONTEXTS
	SET
		Descriptive_Flexfield_Name	=	NVL(CAPBRIMP_MAPPING.RenameStr(SUBSTR(Descriptive_Flexfield_Name,7,4),Descriptive_Flexfield_Name),
										CAPBRIMP_MAPPING.RenameStr(SUBSTR(Descriptive_Flexfield_Name,11,4),Descriptive_Flexfield_Name))
	WHERE	(Application_ID,Descriptive_Flexfield_Name,Descriptive_Flex_Context_Code) IN
		(
			SELECT
				Application_ID,Descriptive_Flexfield_Name,Descriptive_Flex_Context_Code
			FROM
				CRST_FND_CONC_PROG_CONTEXT
		);
	
	UPDATE
		FND_DESCR_FLEX_CONTEXTS
	SET
		Descriptive_Flexfield_Name	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(Descriptive_Flexfield_Name,11,4),Descriptive_Flexfield_Name)
	WHERE	(Application_ID,Descriptive_Flexfield_Name,Descriptive_Flex_Context_Code) IN
		(
			SELECT
				Application_ID,Descriptive_Flexfield_Name,Descriptive_Flex_Context_Code
			FROM
				CRST_FND_CONC_PROG_CONTEXT
		);

	FOR C1 IN Cur_F LOOP
		BEGIN
			INSERT INTO FND_DESCR_FLEX_CONTEXTS
			VALUES
			(
			C1.APPLICATION_ID,
			C1.DESCRIPTIVE_FLEXFIELD_NAME,
			C1.DESCRIPTIVE_FLEX_CONTEXT_CODE,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),		--LAST_UPDATE_BY,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),			--CREATED_BY,
			C1.LAST_UPDATE_LOGIN,
			C1.ENABLED_FLAG,
			C1.GLOBAL_FLAG
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	BEGIN
		FOR C2 IN Cur_G LOOP
			UPDATE	CFND_DESCR_FLEX_CONTEXTS
			SET		Descriptive_Flexfield_Name	=	NVL(CAPBRIMP_MAPPING.RenameStr(SUBSTR(Descriptive_Flexfield_Name,7,4),Descriptive_Flexfield_Name),
														CAPBRIMP_MAPPING.RenameStr(SUBSTR(Descriptive_Flexfield_Name,11,4),Descriptive_Flexfield_Name))
			WHERE	Application_ID					=	C2.Application_ID
			AND		Descriptive_Flexfield_Name		=	C2.Descriptive_Flexfield_Name
			AND		Descriptive_Flex_Context_Code	=	C2.Descriptive_Flex_Context_Code
			AND		S_ORG_ID						=	C2.S_ORG_ID
			AND		S_BUSINESS_GROUP_ID				=	C2.S_BUSINESS_GROUP_ID;
			
			UPDATE	CFND_DESCR_FLEX_CONTEXTS
			SET		Descriptive_Flexfield_Name	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(Descriptive_Flexfield_Name,11,4),Descriptive_Flexfield_Name)
			WHERE	Application_ID					=	C2.Application_ID
			AND		Descriptive_Flexfield_Name		=	C2.Descriptive_Flexfield_Name
			AND		Descriptive_Flex_Context_Code	=	C2.Descriptive_Flex_Context_Code
			AND		S_ORG_ID						=	C2.S_ORG_ID
			AND		S_BUSINESS_GROUP_ID				=	C2.S_BUSINESS_GROUP_ID;

			INSERT INTO CFND_DESCR_FLEX_CONTEXTS VALUES
			(
				C2.Application_ID,
				C2.Descriptive_Flexfield_Name,
				C2.Descriptive_Flex_Context_Code,
				CAPBRIMP_MAPPING.GetOrgMapping(C2.S_ORG_ID),
				CAPBRIMP_MAPPING.GetBGMapping(C2.S_BUSINESS_GROUP_ID)
			);
		END LOOP;
	EXCEPTION
		WHEN OTHERS THEN
			DBMS_OUTPUT.Put_Line(SQLERRM);
			lv_Err_Count	:=	lv_Err_Count + 1;
	END;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/

ALTER TRIGGER FND_DESCR_FLEX_CONTEXTS_CT1 ENABLE;
